package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.StudentDto;
import com.woniu.model.dto.UserByMyPersonalBaseDto;
import com.woniu.model.dto.UserByMyPersonalDto;
import com.woniu.model.dto.UserPersonalClassMarginDto;
import com.woniu.model.form.AddPersonalClassMarginForm;
import com.woniu.model.form.PageForm;
import com.woniu.model.form.UserAndClassIdForm;
import com.woniu.model.model.YUserPersonalClassMargin;
import com.woniu.model.param.UserByMyPersonalParam;
import com.woniu.service.YUserPersonalClassMarginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yUserPersonalClassMargin")
@Api(tags = "用户私教课余量接口")
public class YUserPersonalClassMarginController {

    @Resource
    private YUserPersonalClassMarginService userPersonalClassMarginService;
    @Resource
    private RedisUtil redisUtil;


    /**
     * 通过用户token查该用户的私课后，点击打开这个私课的详细信息
     *
     * @param token
     * @return
     */
    @GetMapping("/userByMyPersonalBase")
    @ApiOperation("通过用户token查该用户的私课后，点击打开这个私课的详细信息")
    public  Result<UserByMyPersonalBaseDto> userByMyPersonalBase( @RequestHeader String token) {

        //拿id
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        UserByMyPersonalBaseDto dto = userPersonalClassMarginService.userByMyPersonalBase(userId);

        return Result.newInstance(UserByMyPersonalBaseDto.class).setData(dto).setMsg("查询成功").setCode(200);
    }



    /**
     * 通过用户token查该用户的私课
     *
     * @param token
     * @return
     */
    @GetMapping("/userByMyPersonal")
    @ApiOperation("通过用户token查该用户的私课")
    public Result<Page<UserByMyPersonalDto>> userByMyPersonal(PageForm form, @RequestHeader String token) {
        //拿id
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());
        //装id
        UserByMyPersonalParam param = BeanUtil.toBean(form, UserByMyPersonalParam.class);
        param.setUserId(userId);

        Page<UserByMyPersonalDto> page = userPersonalClassMarginService.userByMyPersonal(param);

        return Result.newPageInstance(UserByMyPersonalDto.class).setData(page).setMsg("查询成功").setCode(200);
    }




    /**
     * 新增用户的私教课余量
     * @param form
     * @return
     */
    @PostMapping("/addPersonalClassMargin")
    @ApiOperation("新增用户的私教课余量")
    public Result<?> addPersonalClassMargin(@RequestBody AddPersonalClassMarginForm form) {

        Integer i = userPersonalClassMarginService.addPersonalClassMargin(form);
        if (!i.equals(1)){
            return Result.getResult().setCode(500).setMsg("新增失败");
        }
        return Result.getResult().setMsg("新增成功");
    }

    /**
     * 传用户id和课程id查询私教课余量
     *
     * @param form
     * @return
     * @throws YogaException
     */
    @PostMapping("/loadPersonalMarginByUserAndClassId")
    @ApiOperation("传用户id和课程id查询私教课余量")
    public Result<YUserPersonalClassMargin> loadPersonalMarginByUserAndClassId(@RequestBody UserAndClassIdForm form){

        YUserPersonalClassMargin userPersonalClassMargin = userPersonalClassMarginService.loadPersonalMarginByUserAndClassId(form);
        return Result.newInstance(YUserPersonalClassMargin.class).setData(userPersonalClassMargin).setMsg("查询成功");
    }

    /**
     * 私教课余量减一
     *
     * @param id 私教课余量id
     * @return
     * @throws YogaException
     */
    @PostMapping("/substractPersonalMarginByUserAndClassId")
    @ApiOperation("私教课余量减一")
    public Result<?> substractPersonalMarginByUserAndClassId(@RequestBody Integer id){

        userPersonalClassMarginService.substractPersonalMarginByUserAndClassId(id);

        return Result.getResult().setMsg("减少成功");
    }

    /**
     * 根据教练id查询学员
     * @param coachId
     * @return
     */
    @GetMapping("/loadPersonalStudentIds")
    @ApiOperation("根据教练id查询学员Id")
    public Result<List<Integer>> loadPersonalStudentIds( Integer coachId){
        List<Integer> personalStudents = userPersonalClassMarginService.loadPersonalStudentIds(coachId);
        return Result.newListInstance(Integer.class).setData(personalStudents);
    }

    /**
     * 根据教练id查询学员对象
     * @param token
     * @return
     */
    @GetMapping("/loadPersonalStudent")
    @ApiOperation("根据教练id查询学员对象")
    public Result<List<StudentDto>> loadPersonalStudent(@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        List<StudentDto> personalStudents = userPersonalClassMarginService.loadPersonalStudent(coachId);
        return Result.newListInstance(StudentDto.class).setData(personalStudents);
    }

    /**
     * 根据教练id查询已预约的私教课
     * @param token
     * @return
     */
    @GetMapping("/loadPersonalClassByCoachId")
    @ApiOperation("根据教练id查询已预约的私教课")
    public Result<List<UserPersonalClassMarginDto>> loadPersonalClassByCoachId(@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        List<UserPersonalClassMarginDto> personalStudents = userPersonalClassMarginService.loadPersonalClassByCoachId(coachId);
        return Result.newListInstance(UserPersonalClassMarginDto.class).setData(personalStudents);
    }








}

